<template>
  <el-dialog :visible.sync="value" :before-close="() => $emit('input', false)">
    <template slot="title">
      编辑账户
    </template>
    <el-form :model="form" ref="refForm" :rules="rules" label-width="180px">
      <el-form-item label="账户名">
        <el-input disabled v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="部门岗位设置：" prop="department">
        <el-cascader
          :options="options"
          v-model="form.department"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('input', false)" :loading="loading">取 消</el-button>
      <el-button type="primary" @click="doSave({action: 'saveForm', form: 'refForm'})" :loading="loading">确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {authDepartmentAllGetApi, authAccountEditApi} from '@/services/api/permission';

  import {form, factory} from '@/build';

  export default {

    name: 'edit-account-dialog',

    props: ['value', 'id', 'username', 'jobId','rowData'],

    mixins: [form],

    data() {
      return {
        form: {
          userId: undefined,
          username: undefined,
          password: undefined,
          department: undefined,
          remark: undefined
        },
        options: [],
        rules: {
          username: factory.getRule('adminUsername'),
          // password: factory.getRule('password'),
          department: [
            {required: true, message: '请选择你要复制的岗位'}
          ]
        }
      }
    },
    computed: {
      departmentId() {
        let _departmentId = undefined;
        const {jobId} = this;
        _.map(this.options, (item, index) => {
          const ret = _.find(item, function (o) {
            return _.some(item.children, function (jobItem) {
              return jobItem.value == jobId;
            });
          });
          console.log(ret)

          if(ret){
            _departmentId = item.value
          }
        });
        return _departmentId
      },
    },
    methods: {},
    mounted() {
      this.$nextTick(() => {
        if(!_.isUndefined(this.rowData)){
          const {username,remark,userId} = this.rowData;
          this.form = {...this.form,userId,username,remark}
        }
      });
      this.bindGet(authDepartmentAllGetApi, 'options', {
        format: (list) => {
          return _.map(list, data => {
            return {
              value: data.departmentId,
              label: data.name,
              children: _.map(data.jobList, (childrenInfo) => {
                if(childrenInfo.jobId == this.jobId){
                  this.form.department = [data.departmentId,this.jobId]
                }
                return {
                  value: childrenInfo.jobId,
                  label: childrenInfo.name
                };
              })
            }
          })
        }
      });

      this.bindSave(authAccountEditApi, 'saveForm', 'form', {
        afterSuccess: () => {
          this.$emit('submit');
        }
      });

      this.getAll();
    },
  }
</script>

<style scoped>
</style>
